﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StonePonyMVC.Models;
using System.Xml.Linq;

namespace StonePonyMVC.StonePony.Classes
{
    public class LetterWriter
    {
        private LetterModel _model;

        public LetterWriter(LetterModel model)
        {
            this._model = model;
            this._model.SiteUrl = "www.stone-pony.com.au";
        }

        private static readonly string text_01 = "This message was automatically generated by the "; 
        private static readonly string text_01a= " website";
        private static readonly string text_02 = "Here is a list of items that you enquired about during your last session. If you have any quiries " +
                                                 "about this list, please reply to this email.";
        private static readonly string text_03 = "One of our sales staff will be in contact soon.";
        private static readonly string text_04 = "Thanks,";
        private static readonly string text_05 = "STONE PONY";

        private static readonly string style_01 = "color: #FEF6E1; background-color: transparent; font-family: Arial, 'Lucida Sans Unicode', 'Lucida Grande', " +
                                                  "sans-serif; font-size: 10pt; font-weight: normal;";
        private static readonly string style_02 = "width: 100%; border: 1px solid #43413D; background-color: #444444; border-radius: 5px; " +
                                                  "-webkit-border-radius: 5px; -moz-border-radius: 5px;";
        private static readonly string style_03 = "padding: 10px; text-align: left; font-weight: bolder;";
        private static readonly string style_04 = "width: 100%; text-align: center; margin-bottom: 30px;";
        private static readonly string style_04a = "color: #FEF6E1;";

        private static readonly string style_04l = "width: 100%; text-align: left; margin-bottom: 30px;";
        private static readonly string style_04p = "font-size: 110%; color: #18B0E2;";
        private static readonly string style_04q = "color: #18B0E2; font-size: 110%;";
        private static readonly string style_05 = "margin: 10px 10px; font-size: small;";
        private static readonly string style_06 = "float:left; width: 160px; border: 1px solid #43413D; margin: 5px 20px 0 0; font-weight: bolder; "+
                                                  "text-align: center;";
        private static readonly string style_07 = "background-color: #444444; margin: 0 auto; padding: 0; list-style: none; width: 160px;";
        private static readonly string style_08 = "border-bottom: 2px solid #8C8C8C; width: 160px; margin-left: 0 !important; ";
        private static readonly string style_08a ="border-bottom: 2px solid #8C8C8C; min-height: 2.5em; width: 160px; margin-left: 0 !important; ";
        private static readonly string style_09 = "padding: 4px 0; margin-left: 0 !important; ";

        public string GenerateLetter()
        {
            XNamespace ns = "http://www.w3.org/1999/xhtml";
            var doc = new XDocument(
                new XDeclaration("1.0", "utf-16", "true"),
                new XElement(ns + "html",
                    new XElement("head",
                        new XElement("title", "Letter")
                        ),
                    new XElement("body",
                        new XAttribute("style", style_01),
                        new XElement("div",
                            new XAttribute("style", style_02),
                            new XElement("div",
                                new XAttribute("style", style_03),
                                new XElement("div",
                                    new XAttribute("style", style_04),
                                    new XElement("small",
                                        new XElement("span", text_01),
                                        new XElement("a",
                                            new XAttribute("href", "#"),
                                            new XAttribute("style", style_04a),
                                            this._model.SiteUrl),
                                        new XElement("span", text_01a)
                                )),
                                new XElement("div",
                                    new XAttribute("style", style_04l),
                                    new XElement("p",
                                        new XAttribute("style", style_04p),
                                        String.Format("Dear {0},", this._model.FirstName.ToUpper())),
                                    new XElement("p", text_02),
                                    new XElement("p", text_03),
                                    new XElement("br"),
                                    new XElement("p", text_04),
                                    new XElement("p",
                                        new XAttribute("style", style_04q),
                                        text_05)
                        ))),
                        CreateEnquiryList()
                )));
            return doc.ToString();
        }

        private XElement CreateEnquiryList()
        {
            var rzlt = new XElement("div",
                new XAttribute("style", style_05)
                );
            foreach (ImageDescriptor image in this._model.Items)
                rzlt.Add(CreateItemDescriptor(image));

            return rzlt;
        }

        private XElement CreateItemDescriptor(ImageDescriptor image)
        {
            var rzlt = new XElement("div",
                new XAttribute("style", style_06),
                new XElement("ul",
                    new XAttribute("style", style_07),
                    new XElement("li", String.Format("Shipment: {0}", image.ItemShipment),
                        new XAttribute("style", style_08)),
                    new XElement("li", image.ItemStockCode,
                        new XAttribute("style", style_08)),
                    new XElement("li", image.ItemTitle.ToUpper(),
                        new XAttribute("style", style_08a)),
                    new XElement("li", 
                        new XAttribute("style", style_09),
                        new XElement("img",
                            new XAttribute("src", image.SmallImageUrlFull),
                            new XAttribute("alt", image.ItemTitle)
                        ))
                ));
            return rzlt;
        }
    }
}